const Mock = require('mockjs')
const Random = Mock.Random;

const List = []
const count = 100
for (let i = 0; i < count; i++) {
  // 演示列表
  List.push(Mock.mock({
    id: '@guid()',
    code: '@string(16)',
    name: '@cword(3, 5)',
    'type|1': ['TYPE_A', 'TYPE_B', 'TYPE_C', 'TYPE_D'],
    "status|1": ['状态1', '状态2', '状态3'],
    startTime: '@datetime()',
    endTime: '@datetime()',
    supplyOrigin: '@county(true)',
    productBrand: '@cword(3, 5)',
    supplier: '@cword(3, 5)',
    group: '@cword(3, 5)',
    sendWay: '@cword(3, 5)',
    commodityNo: '@string(16)',
    sendAddress: '@county(true)',
    exchangeIntro: '@csentence(0, 30)',
    mailIntro: '@csentence(0, 30)',
    mailPrice: '@float(1, 10000, 0, 2)',
    commodityIntro: '@csentence(0, 30)',
    detailPic: Random.image('1080x720'),
    'inventroy|1-10000': 1,
    'coBuyPeople|1-100': 1,
    marketPrice: '@float(1, 10000, 0, 2)',
    buyPrice: '@float(1, 10000, 0, 2)',
    coBuyPrice: '@float(1, 10000, 0, 2)',
    sizeIntro: '@csentence(0, 30)',
    sku: '@string(16)',
    createTime: '@datetime()',
    createUser: '@cname()',
    modifyTime: '@datetime()',
    modifyUser: '@cname()'
  }))
}

// 通用模板API
const fastAPI = {
  // 获取数据列表
  pageIndex: (config) => {
    const { name, currentPage = 1, pageSize = 10 } = config.query;

    const mockList = List.filter(item => {
      if (name && item.name !== name) return false
      return true
    })
    const page = Number(currentPage);
    const limit = Number(pageSize);
    const pageList = mockList.filter((item, index) => 
        index < limit * page && index >= limit * (page - 1)
    )

    return {
      currentPage: page,
      pageSize: limit,
      data: pageList,
      code: 200,
      msg: '成功',
      total: List.length,
    };
  },
  // 查询数据详情
  queryById: (config) => {
    const { id } = config.query || {};

    const result = List.find((item) => item.id == id);
    return {
      data: result,
      code: 200,
      msg: '成功'
    };
  },
  // 成功处理数据
  success: (config) => {
    return {
      code: 200,
      msg: '成功'
    };
  }
}

module.exports = [
  {
    url: '/list/index',
    type: 'get',
    response: fastAPI.pageIndex
  },
  {
    url: '/list/info',
    type: 'get',
    response: fastAPI.queryById
  },
  {
    url: '/list/add',
    type: 'post',
    response: _ => fastAPI.success()
  },
  {
    url: '/list/delete',
    type: 'post',
    response: _ => fastAPI.success()
  }
]